/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
/*
 * PredToolkit.fx
 *
 * Created on Apr 13, 2008, 4:04:09 PM
 */

package avgengine.pred;

/**
 * @author ertri
 */

class PredToolkit {
    operation printClause(c:Clause):String;
    operation printLiteral(l:Literal):String;
    operation printTerm(t:Term):String;
    operation printSymbol(s:Symbol):String;
}

operation PredToolkit.printClause(c:Clause) {
    var str= "{printLiteral(c.head)} :- ";
    for(i in c.tail) {
        str = "{str}{printLiteral(i)} ";
    }
    str="{str}.";
    return str;
    
}
operation PredToolkit.printLiteral(l:Literal) {
    if(sizeof l.params == 0) {
        return printSymbol(l.p);
    } else {
        var str = if l.sign<0 then '~' else '';
        str = "{str}{printSymbol(l.p)}( ";
        for(i in l.params) {
            str = "{str}{printTerm(i)} ";
        }
        str="{str})";
        return str;
    }
}
operation PredToolkit.printTerm(t:Term) {
    if(t instanceof Variable) {
        return printSymbol((Variable)t);
    } else {
        var str = printSymbol(((FunctionTerm)t).f);
        str = "{str}(";
        for(i in((FunctionTerm)t).params) {
            str = "{str}{printTerm(i)}";
        }
        str = "{str})";
        return str;
    }
}
operation PredToolkit.printSymbol(s:Symbol) {
    return "{s.n}";
}
